package com.bite.system.Service.ServiceImpl;

import com.bite.system.DTO.UserDTO;
import com.bite.system.DTO.UserUpdateStatusDTO;
import com.bite.system.Mapper.UserMapper;
import com.bite.system.Service.UserService;
import com.bite.system.VO.UserVO;
import com.bite.system.domain.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import domain.R;
import domain.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public TableDataInfo list(UserDTO userDTO) {
        PageHelper.startPage(userDTO.getPage(), userDTO.getPageSize());
        List<UserVO> list = userMapper.list(userDTO);
        if (list == null || list.size() == 0){
            return TableDataInfo.empty();
        }
        Page page = (Page)list;
        return TableDataInfo.success(page.getResult(),page.getTotal());
    }

    @Override
    public R<Void> updateStatus(UserUpdateStatusDTO updateStatusDTO) {
        Long userId = updateStatusDTO.getUserId();
        User user = userMapper.selectById(userId);
        if (user == null){
            throw new RuntimeException("该用户信息不存在");
        }
        Integer status = updateStatusDTO.getStatus();
        if (status == null){
            throw new RuntimeException("状态不能为空");
        }
        user.setStatus(status);
        int i = userMapper.updateById(user);
        if (i > 0){
            return R.success();
        }
        return R.failed();
    }
}
